package com.senken.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.senken.mapper.SkUserMapper;
import com.senken.commons.entity.FidStateAndPage;
import com.senken.commons.result.Result;
import com.senken.entity.SkUser;
import com.senken.service.ISkUserRoleService;
import com.senken.service.ISkUserService;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;
import com.baomidou.framework.service.impl.SuperServiceImpl;

/**
 *
 * SkUser 表数据服务层接口实现类
 *
 */
@Service
public class SkUserServiceImpl extends SuperServiceImpl<SkUserMapper, SkUser> implements ISkUserService {

	private static final int NUM=10;
	@Autowired
	private ISkUserRoleService userroleService;
	@Autowired
	private SkUserMapper usermapper;
	
	@Override
	public SkUser selectByid(int id){
		return this.selectById((long) (id));
	}
	
	@Override
	public SkUser selectByUsername(String username){
		SkUser user = new SkUser();
		user.setUsername(username);
		return this.selectOne(user);
	}
	
	@Override
	public boolean addUser(String username,String email,String password,String nickname,String phone,int role){
		SkUser user = new SkUser();
		user.setEmail(email);
		user.setNickname(nickname);
		user.setUsername(username);
		user.setPhone(phone);
		user.setPassword(password);
		user.setLogintime(new Date());
		user.setLogout(new Date());
		if(insert(user)){
			if(userroleService.add(user.getUserId(), role)){
				return true;
			};
			return false;
		}
		return false;
	}
	
	@Override
	public Result updatePwd(String username,String newpassword,String oldpassword,String pwd){
		Result result = new Result();
		SkUser user = this.selectByUsername(username);
		 if(!(newpassword.equals(pwd))){
		    	result.setSuccess(false);
		    	result.setMsg("两次密码不一致");
		    	return result;
		    }
	    if(!(user.getPassword().equals(oldpassword))){
	    	result.setSuccess(false);
	    	result.setMsg("密码错误");
	    	return result;
	    }
	   
	    user.setPassword(newpassword);
	    if(this.updateById(user)){
	    	result.setSuccess(true);
	    	result.setMsg("更改成功");
	    }
	    else {
			result.setSuccess(false);
			result.setMsg("系统异常稍后重试");
		}
		return result;
		
	}
	
	@Override
	public void updatelogintime(String username){
		SkUser user = this.selectByUsername(username);
		user.setLogintime(new Date());
		this.updateById(user);
	}
	
	@Override
	public void updatelogoutime(String username){
		SkUser user = this.selectByUsername(username);
		user.setLogout(new Date());
		this.updateById(user);
	}
	
	@Override
	public List<SkUser> findUserByRole(int roleid){
		return usermapper.findUserByRole(roleid);
	}
	
	@Override
	public SkUser findOne(int roleid){
		return usermapper.findOne(roleid);
	}
	
	@Override
	public List<SkUser> findAll(int page){
		FidStateAndPage fidStateAndPage = new FidStateAndPage();
		fidStateAndPage.setNum(NUM);
		fidStateAndPage.setStart((page-1)*NUM);
		return usermapper.findAll(fidStateAndPage);
	}
	
	@Override
	public int counts(){
		SkUser user = new SkUser();
		return this.selectCount(user);
	}
}









